package com.ht.medicaltreatment.viewmodel

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import androidx.lifecycle.ViewModel
import com.blankj.utilcode.util.ToastUtils
import com.gym.baselib.utils.ProgressUtils
import com.ht.medicaltreatment.api.RetrofitUtils
import com.xgst.driverlesscars.api.ApiService

open abstract class BaseViewModel:ViewModel(), LifecycleOwner {
     open val mApi = RetrofitUtils.initRetrofit(ApiService::class.java)
     private val mLifecycle: LifecycleRegistry = LifecycleRegistry(this)

     init {
          mLifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
          mLifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
     }

     fun dismissProgress(toastMessage:String = ""){
          ProgressUtils.dismiss()
          if(toastMessage.isNotEmpty()){
               ToastUtils.showShort(toastMessage)
          }
     }

     fun showProgress(){
          ProgressUtils.showProgress()
     }

     override fun onCleared() {
          super.onCleared()
          mLifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
     }


     override fun getLifecycle() = mLifecycle
}